﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PPI.Entities.Inspections;

namespace PPIControls.Inspection.Details
{
    public partial class InspectionPoleList : UserControl
    {
        public InspectionPoleList()
        {
            InitializeComponent();
        }

        private void btnShowInspectionToAdd_Click(object sender, EventArgs e)
        {
            this.txtInspectionId.Visible = true;
            this.btnSaveInspection.Visible = true;
            this.btnShowInspectionToAdd.Visible = false;
        }

        private void btnSaveInspection_Click(object sender, EventArgs e)
        {
            if(txtInspectionId.Value > 0)
                OnClickedAddInspection(sender,e);

            this.btnShowInspectionToAdd.Visible = true;
            this.txtInspectionId.Visible = false;
            this.btnSaveInspection.Visible = false;
        }

        public void SetInspections(List<EInspection> inspections)
        {
            lstInspectionPoles.BeginUpdate();
            lstInspectionPoles.DataSource = null;
            lstInspectionPoles.DataSource = inspections;
            lstInspectionPoles.EndUpdate();
        }

        public EInspection GetSelectedInspection()
        {
            if(lstInspectionPoles.SelectedItem != null)
            {
                return (EInspection) lstInspectionPoles.SelectedItem;

            }else
            {
                return null;
            }
        }

        public double GetInspectionToAdd()
        {
            double inspectionIdToAdd = txtInspectionId.Value;
            txtInspectionId.Value = 0;
            return inspectionIdToAdd;
        }

        public event EventHandler<EventArgs> ClickedInspection;
        public event EventHandler<EventArgs> ClickedAddInspection;

        private void OnClickedInspection(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (ClickedInspection != null)
            {
                ClickedInspection(o, e);
            }
        }
        private void OnClickedAddInspection(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (ClickedAddInspection != null)
            {
                ClickedAddInspection(o, e);
            }
        }

        private void lstInspectionPoles_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(GetSelectedInspection() != null)
            {
                OnClickedInspection(sender,e);
            }
        }



    }
}
